/**
 * Created by lixiaodi on 2018/10/10.
 */
mini.parse();
var form = new mini.Form("form1");
//下拉框赋值
var modularInput = mini.getByName("modular.modularId");   //模块
var menusInput = mini.getByName("parId");                 //菜单
var parName = mini.getByName("parName");
var menuName = mini.getByName('menuName');
var menuCode = mini.getByName('menuCode');
//初始化(得到模块选择控件)
$(function(){
    modularInput.setUrl(ajaxUrl + "/modular/list.do");  //模块Url
    //menusInput.setUrl(ajaxUrl + "/menus/queryListPar.do");   //菜单Url
});
//选择父级菜单
function changePar(e){
	//var btnEdit = this;
	mini.open({
        url: bootPATH + "../frame/frame1/pages/alertBox/parMenusWin.html",
        title: "选择父级菜单",
        width: 500,
        height: 360,
        ondestroy: function (action) {
            //if (action == "close") return false;
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);    //必须
                if (data) {
                	menusInput.setValue(data.menuId);
                	menusInput.setText(data.menuName);
                }
            }
        }
    });
}
//校验菜单名称是否重复
function onblurEvent(){
	//得到当前填写的菜单名称
	var menuNameVal = menuName.getValue();
	console.log(menuNameVal);
	if(menuNameVal){
		$.ajax({
			type    : 'POST',
			url     : ajaxUrl + '/menus/findMenuName.do',
			data    : {
				menuName : menuNameVal
			},
			success : function (data){
				if(data.state == 'error'){
					mini.alert(data.message);
					mini.get("menuName").setValue('');
				}
			},
			error : function(message) {
				mini.alert(message);
			}
		})
	}else{
		mini.alert('请填写菜单名称');
	}
}
//校验菜单编号是否重复
function findMenuCode(){
	var ids = form.getData();
	var menuCodeVal = ids.menuCode;
	$.ajax({
		type : 'post',
		url : ajaxUrl + '/menus/findMenuCode.do',
		data : {
			menuCode : menuCodeVal
		},
		success : function(data){
			if(data.state == 'error'){
				mini.alert(data.message)
				mini.get("menuCode").setValue('');
			}
		},
		error : function(message){
			mini.alert(message)
		}
	})
}

//保存
function SaveData() {
	//验证
    form.validate();
    if (form.isValid() == false) return;

    var ids = form.getData();
    console.log(ids);
    if(ids.parId == 0 || ids.parId == ""){
        ids.parId = 0;
    }
    var menus = {
        menuId : ids.menuId,
        menuCode : ids.menuCode,
        menuOrderId : ids.menuOrderId,
        menuName : ids.menuName,
        menuLcon: ids.menuLcon,
        menuUrl : ids.menuUrl,
        parId: ids.parId,
        modularId: ids.modular.modularId,
        parName : ids.parName
    };
    console.log(menus);
    menus = JSON.stringify(menus);
    $.ajax({
        url: ajaxUrl + "/menus/insertMenus.do",
        type: 'POST',
        data: menus,
        contentType : "application/json",
        success: function (data) {
            console.log(data);
            CloseWindow("save");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            CloseWindow();
        }
    });
}
//赋值
function SetData(data) {

    //如果编辑
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        console.log(data);
        var menuId = data.id;
        $.ajax({
            url: ajaxUrl + "/menus/findByMenusId.do?menuId=" + menuId,
            type : "POST",
            success: function (data) {
                console.log(data);
            	var o = mini.decode(data);
                form.setData(o.data);
                menusInput.setValue(o.data.parId);
            	menusInput.setText(o.data.parName);
            },
            error:function(data){
                mini.alert('失败');
            }
        });
    //如果新增
    }else if(data.action == "new"){
    	console.log(data);
        var parentNodeIds = data.parentNodes;
        modularInput.setValue(parentNodeIds.modularId);
        menusInput.setValue(parentNodeIds.menusId);
        menusInput.setText(data.parentMenuText);
        //parName.setValue(data.parentText);
    }
}

function GetData() {
    var o = form.getData();
    return o;
}
//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
var modularText;
var modularValues;
var menusValues;
var menusText;
//当模块选择改变时
function onModularChanged(e) {
    modularValues = modularInput.getValue();
    modularText = modularInput.getText();
    console.log(modularText)
    return modularText,modularValues;
}
//当菜单选择的时候
function onMenusChanged(){
    var menusValue = menusInput.getValue();
    menusValues = menusInput.getValue();
    menusText = menusInput.getText();
    console.log(menusValues)
    return menusValues
}
//校验模块必输
function onNotSpaceValidation(e) {
    if (e.isValid) {
        if (!e.value) {
            e.errorText = "不能为空";
            e.isValid = false;
        }
    }
}